#include <iostream>
#include <sys/socket.h>
#include <cstdlib>
#include <cstring>
#include <arpa/inet.h>

#define SERVER_PORT 9999
#define BUFFER_SIZE 128

int main()
{
    // 创建套接字的句柄
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        perror("socket error");
        exit(-1);
    }

    struct sockaddr_in serverAddr;
    // 清除脏数据
    memset(&serverAddr, 0, sizeof(serverAddr));

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
    const char *serverIp = "192.168.27.129";
    inet_pton(sockfd, serverIp, &(serverAddr.sin_addr.s_addr));

    // 连接服务器
    // connect(sockfd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr));

    char sendBuffer[BUFFER_SIZE] = {0};
    while (1)
    {
        printf("请输入信息:");
        scanf("%s", sendBuffer);
        sendto(sockfd, sendBuffer, strlen(sendBuffer), 0, (const struct sockaddr *)&serverAddr, sizeof(serverAddr));
    }

    return 0;
}